#ifndef N0THREAD_H
#define N0THREAD_H

#include "N0Types.h"

N0Int32 N0ThreadSafeIncrement(N0Int32 *ptr);
N0Int32 N0ThreadSafeDecrement(N0Int32 *ptr);

inline N0Int32 N0ThreadId() 
{
#ifdef WINDOWS
    return GetCurrentThreadId();
#else
    N0MustBeImplemented
#endif
}

class N0Lock {
    volatile N0Int32 m_lock;
public:
    N0Lock() : m_lock(0) { }
    
    void lock()
    {
       begin:
        m_lock = N0ThreadId();
        if (m_lock==N0ThreadId())
            return;
        while(m_lock)
        {
            N0Sleep(0.001);
        }
        goto begin;
    }

    void unlock()
    {
        m_lock = 0;
    }
};

#endif //N0THREAD_H
